Completed
Push — master ( 89cc87...7d378e )
by Sander
11s
created

angular.controller(ꞌSettingsCtrlꞌ)   B

Complexity

Conditions 6
Paths 25

Size

Total Lines 225

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
c 3
b 0
f 0
nc 25
nop 11
dl 0
loc 225
rs 8.1463

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
27
	/**
28
	 * @ngdoc function
29
	 * @name passmanApp.controller:SettingsCtrl
30
	 * @description
31
	 * # SettingsCtrl
32
	 * Controller of the passmanApp
33
	 */
34
	angular.module('passmanApp')
35
		.controller('SettingsCtrl', ['$scope', '$rootScope', 'SettingsService', 'VaultService', 'CredentialService', '$location', '$routeParams', '$http', 'EncryptService', 'NotificationService', '$sce',
36
			function ($scope, $rootScope, SettingsService, VaultService, CredentialService, $location, $routeParams, $http, EncryptService, NotificationService, $sce) {
37
				$scope.vault_settings = {};
38
				$scope.new_vault_name = '';
39
				$scope.active_vault = VaultService.getActiveVault();
40
				if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
41
					if (!$scope.active_vault) {
42
						$location.path('/');
43
						return;
44
					}
45
				} else {
46
					if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
47
						var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
48
						_vault.vaultKey = SettingsService.getSetting('defaultVaultPass');
49
						VaultService.setActiveVault(_vault);
50
						$scope.active_vault = _vault;
51
					}
52
				}
53
54
				VaultService.getVault($scope.active_vault).then(function (vault) {
55
					vault.vaultKey = VaultService.getActiveVault().vaultKey;
56
					delete vault.credentials;
57
					VaultService.setActiveVault(vault);
58
					$scope.vault_settings = vault.vault_settings;
59
					if(!$scope.vault_settings.hasOwnProperty('pwSettings')){
60
						$scope.vault_settings.pwSettings = {
61
							'length': 12,
62
							'useUppercase': true,
63
							'useLowercase': true,
64
							'useDigits': true,
65
							'useSpecialChars': true,
66
							'minimumDigitCount': 3,
67
							'avoidAmbiguousCharacters': false,
68
							'requireEveryCharType': true,
69
							'generateOnCreate': true
70
						};
71
					}
72
				});
73
74
75
76
				var http = location.protocol, slashes = http.concat("//"), host = slashes.concat(window.location.hostname), complete = host + location.pathname;
77
				$scope.bookmarklet = $sce.trustAsHtml("<a class=\"button\" href=\"javascript:(function(){var a=window,b=document,c=encodeURIComponent,e=c(document.title),d=a.open('" + complete + "bookmarklet?url='+c(b.location)+'&title='+e,'bkmk_popup','left='+((a.screenX||a.screenLeft)+10)+',top='+((a.screenY||a.screenTop)+10)+',height=750px,width=475px,resizable=0,alwaysRaised=1');a.setTimeout(function(){d.focus()},300);})();\">Save in passman</a>");
78
79
80
				$scope.saveVaultSettings = function () {
81
					var _vault = $scope.active_vault;
82
					_vault.name = $scope.new_vault_name;
83
					_vault.vault_settings = angular.copy($scope.vault_settings);
84
					VaultService.updateVault(_vault).then(function () {
85
						//VaultService.setActiveVault(_vault);
86
						$scope.active_vault.name = angular.copy(_vault.name);
87
						NotificationService.showNotification('Settings saved', 5000);
88
					});
89
				};
90
91
92
				$scope.tabs = [
93
					{
94
						title: 'General settings',
95
						url: 'views/partials/forms/settings/general_settings.html'
96
					},
97
					{
98
						title: 'Password Audit',
99
						url: 'views/partials/forms/settings/tool.html'
100
101
					},
102
					{
103
						title: 'Password settings',
104
						url: 'views/partials/forms/settings/password_settings.html'
105
106
					},
107
					{
108
						title: 'Import credentials',
109
						url: 'views/partials/forms/settings/import.html'
110
111
					},
112
					{
113
						title: 'Export credentials',
114
						url: 'views/partials/forms/settings/export.html'
115
116
					},
117
					{
118
						title: 'Sharing',
119
						url: 'views/partials/forms/settings/sharing.html'
120
					}
121
				];
122
123
				$scope.currentTab = $scope.tabs[0];
124
125
				$scope.onClickTab = function (tab) {
126
					$scope.currentTab = tab;
127
				};
128
129
				$scope.isActiveTab = function (tab) {
130
					return tab.url === $scope.currentTab.url;
131
				};
132
133
				var getPassmanVersion = function () {
134
					var url = OC.generateUrl('apps/passman/api/internal/version');
135
					$http.get(url).then(function (result) {
136
						$scope.passman_version = result.data.version;
137
					});
138
				};
139
				getPassmanVersion();
140
141
				$scope.$watch(function () {
142
					return VaultService.getActiveVault();
143
				}, function (vault) {
144
					if (vault) {
145
						$scope.active_vault = vault;
146
					}
147
				});
148
149
				$rootScope.$on('logout', function () {
150
					$scope.selectedVault = false;
151
				});
152
				$scope.startScan = function (minStrength) {
153
					VaultService.getVault($scope.active_vault).then(function (vault) {
154
						var results = [];
155
						for (var i = 0; i < vault.credentials.length; i++) {
156
							var c = angular.copy(vault.credentials[i]);
157
							if (c.password && c.hidden === 0) {
158
								try {
159
									c = CredentialService.decryptCredential(c);
160
									if (c.password) {
161
										var zxcvbn_result = zxcvbn(c.password);
162
										if (zxcvbn_result.score <= minStrength) {
163
											results.push({
164
												credential_id: c.credential_id,
165
												label: c.label,
166
												password: c.password,
167
												password_zxcvbn_result: zxcvbn_result
168
											});
169
										}
170
									}
171
								} catch (e){
172
									console.warn(e);
173
								}
174
175
							}
176
							//@todo loop custom fields (if any and check secret fields
177
						}
178
						$scope.scan_result = results;
179
					});
180
				};
181
182
183
				$scope.cur_state = {};
184
185
186
				$scope.$on("$locationChangeStart", function(event) {
187
					if($scope.change_pw){
188
						if($scope.change_pw.total > 0 && $scope.change_pw.done < $scope.change_pw.total){
189
							if(!confirm("Are you sure you want to leave?\nThis will corrupt all your credentials")){
190
								event.preventDefault();
191
							}
192
						}
193
					}
194
				});
195
196
197
				$scope.changeVaultPassword = function (oldVaultPass, newVaultPass, newVaultPass2) {
198
					if (oldVaultPass !== VaultService.getActiveVault().vaultKey) {
199
						$scope.error = 'Your old password is incorrect!';
200
						return;
201
					}
202
					if (newVaultPass !== newVaultPass2) {
203
						$scope.error = 'New passwords do not match!';
204
						return;
205
					}
206
					VaultService.getVault($scope.active_vault).then(function (vault) {
207
						var _selected_credentials = [];
208
						if (vault.credentials.length === 0) {
209
							$location.path('/');
210
						}
211
						for (var i = 0; i < vault.credentials.length; i++) {
212
							var _credential = vault.credentials[i];
213
							if (_credential.shared_key === null || _credential.shared_key === '') {
214
								_selected_credentials.push(_credential);
215
							}
216
						}
217
						$scope.change_pw = {
218
							percent: 0,
219
							done: 0,
220
							total: _selected_credentials.length
221
						};
222
						var changeCredential = function (index, oldVaultPass, newVaultPass) {
223
							CredentialService.reencryptCredential(_selected_credentials[index].guid, oldVaultPass, newVaultPass).progress(function (data) {
224
								$scope.cur_state = data;
225
							}).then(function () {
226
								var percent = index / _selected_credentials.length * 100;
227
								$scope.change_pw = {
228
									percent: percent,
229
									done: index + 1,
230
									total: _selected_credentials.length
231
								};
232
								if (index < _selected_credentials.length - 1) {
233
									changeCredential(index + 1, oldVaultPass, newVaultPass);
234
								} else {
235
									vault.private_sharing_key = EncryptService.decryptString(angular.copy(vault.private_sharing_key), oldVaultPass);
236
									vault.private_sharing_key = EncryptService.encryptString(vault.private_sharing_key, newVaultPass);
237
									VaultService.updateSharingKeys(vault).then(function () {
238
										$rootScope.$broadcast('logout');
239
										NotificationService.showNotification('Please login with your new vault password', 5000);
240
									});
241
								}
242
							});
243
						};
244
						changeCredential(0, VaultService.getActiveVault().vaultKey, newVaultPass);
245
246
					});
247
				};
248
249
				$rootScope.$on('logout', function () {
250
					$scope.active_vault = null;
251
					VaultService.setActiveVault(null);
252
					$location.path('/');
253
254
				});
255
256
				$scope.cancel = function () {
257
					$location.path('/vault/' + $routeParams.vault_id);
258
				};
259
260
			}]);
261
262
}());